package com.ouc.eddyAlgorithm.dao.impl;


import com.ouc.common.constant.VariableName;
import com.ouc.common.entity.NcData;
import com.ouc.common.ncRead.NcDao;
import com.ouc.common.utils.ArrayUtils;
import com.ouc.common.utils.nc.NcUtil;
import com.ouc.eddyAlgorithm.dao.OwDao;
import com.ouc.eddyAlgorithm.entity.WGridData;
import com.ouc.eddyAlgorithm.utils.OWEddyDetectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import ucar.ma2.InvalidRangeException;
import ucar.nc2.NetcdfFile;
import ucar.nc2.Variable;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Repository
public class OwDaoImpl implements OwDao {
  @Autowired
  private NcDao ncDao;

  @Override
  public WGridData getWGridData(NetcdfFile file) throws IOException, InvalidRangeException {
    WGridData wGridData = new WGridData();
    Variable uFlow = ncDao.getVariable(file, VariableName.UFLOW);
    Variable vFlow = ncDao.getVariable(file, VariableName.VFLOW);
    Variable lon = ncDao.getVariable(file, VariableName.LON);
    Variable lat = ncDao.getVariable(file, VariableName.LAT);
    List<Variable> variables = Arrays.asList(lon, lat, uFlow, vFlow);
    float[] listLon = null;
    float[] listLat = null;
    float[][] u2DData = null;
    float[][] v2DData = null;
    float[][] w2DData;
    Map<String, NcData> map = NcUtil.getAllDataOfSlice(variables);
    for (Map.Entry<String, NcData> entry : map.entrySet()) {
      if (entry.getKey().matches(".*lon.*")) {
        listLon = entry.getValue().getData1D();
        continue;
      }
      if (entry.getKey().matches(".*lat.*")) {
        listLat = entry.getValue().getData1D();
        continue;
      }
      if (entry.getKey().matches(".*u.*")) {
        u2DData = entry.getValue().getData2D();
        continue;
      }
      if (entry.getKey().matches(".*v.*")) {
        v2DData = entry.getValue().getData2D();
        continue;
      }
    }
    w2DData = new float[u2DData.length][u2DData[0].length];

    for (int i = 0; i < u2DData.length; i++) {
      for (int j = 0; j < u2DData[i].length; j++) {
        //四条边界赋值为NaN
        if (i == 0 || i == u2DData.length - 1) {
          w2DData[i][j] = Float.NaN;
          continue;
        }
        if (j == 0 || j == u2DData[i].length - 1) {
          w2DData[i][j] = Float.NaN;
          continue;
        }
        w2DData[i][j] = OWEddyDetectUtil.getOneW(v2DData[i][j - 1], v2DData[i][j + 1], u2DData[i][j - 1], u2DData[i - 1][j], u2DData[i][j + 1],
            u2DData[i + 1][j], listLon[j - 1], listLon[j + 1], listLat[i - 1], listLat[i + 1]);


      }
    }

    wGridData.setListLon(listLon);
    wGridData.setListLat(listLat);
    wGridData.setListUFlow(ArrayUtils.flat2D(u2DData));
    wGridData.setListVFlow(ArrayUtils.flat2D(v2DData));
    wGridData.setValues(ArrayUtils.flat2D(w2DData));
    wGridData.setW0(OWEddyDetectUtil.getWStandard(w2DData));
    wGridData.setVariableName("w");

    return wGridData;
  }

}
